package org.example.oop.thread;

import lombok.SneakyThrows;

import java.text.DecimalFormat;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * org.example.oop.thread
 *
 * @author moLei
 * @description 100元分为3个红包，随机分配，五个人来抢夺红包，打印结果
 * @date 2025/6/25
 */
public class RedPacketExercises {

    /**
     * 红包个数
     */
    private static int redCount = 3;

    /**
     * 红包金额
     */
    private static double redPacketAmount = 100;

    /**
     * 当前红包剩余金额
     */
    private static double currentAmount = redPacketAmount;

    /**
     * 红包抢夺
     */
    public static void getThreadRedPacket() {
        for (int i = 0; i < 5; i++) {
            // 创建线程
            new Thread(new Runnable() {
                @Override
                @SneakyThrows
                public void run() {
                    getRedPacketByThread();
                }
            }).start();
        }
    }

    private synchronized static void getRedPacketByThread() {
        // 获取金额格式化对象，只取小数点后两位
        DecimalFormat df = new DecimalFormat("#.00");
        // 判断红包个数是否抢夺完毕
        if (redCount < 1) {
            System.out.println(Thread.currentThread().getName() + "没有抢夺到红包");
        } else if (redCount == 1) {
            // 最后一个红包，直接返回当前剩余金额
            System.out.println(Thread.currentThread().getName() + "抢夺到红包，红包金额：" + Double.parseDouble(df.format(currentAmount)) + "   剩余金额：" + 0.00);
        }else {
            // 获取红包金额，随机生成
            double nowAmount = Double.parseDouble(df.format(Math.random() * currentAmount));
            currentAmount -= nowAmount;
            System.out.println(Thread.currentThread().getName() + "抢夺到红包，红包金额：" + Double.parseDouble(df.format(nowAmount)) + "   剩余金额：" + Double.parseDouble(df.format(currentAmount)));
        }
        // 红包数减一
        redCount --;
    }

    /**
     * 使用队列来存储红包
     */
    private static ArrayBlockingQueue redPacket = new ArrayBlockingQueue(3);

    public static void main(String[] args) throws InterruptedException {

        // initRedPacket();
        // new Thread(() -> getRedPacket()).start();
        // new Thread(() -> getRedPacket()).start();
        // new Thread(() -> getRedPacket()).start();

        System.out.println("通过线程进行获取");
        getThreadRedPacket();

    }

    /**
     * 初始化红包
     */
    public static void initRedPacket() throws InterruptedException {
        // 生成一个随机数，获取第一个红包的金额
        double firstRedPacket = Math.random() * 100;
        // 获取小数点后两位
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        firstRedPacket = Double.parseDouble(decimalFormat.format(firstRedPacket));

        // 计算第二个红包的金额
        double secondRedPacket = Double.parseDouble(decimalFormat.format(Math.random() * (100 - firstRedPacket)));

        // 计算第三个红包的金额
        double thirdRedPacket = Double.parseDouble(decimalFormat.format(100 - firstRedPacket - secondRedPacket));

        // 将红包放入阻塞队列中
        redPacket.put(firstRedPacket);
        redPacket.put(secondRedPacket);
        redPacket.put(thirdRedPacket);
    }

    /**
     * 抢夺红包，使用队列的方式则没办法打印没抢到的两个人信息
     */
    public static void getRedPacket() {
        try {
            System.out.println(Thread.currentThread().getName() + "获得红包：" + redPacket.take() + "元");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
